﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class AccountTypeProcessBll 
    {
        public static AccountTypeCls[] ReadingAccountType(string SiteId,string ParentAccountType)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query = "select *,TotalRole=(select count(RoleAccountTypeId) from TableRoleAccountType where TableRoleAccountType.AccountType=TableAccountType.AccountType) from TableAccountType where 1=1 ";
            if (!string.IsNullOrEmpty(ParentAccountType))
            {
                Query += " and ParentAccountType='" + ParentAccountType + "'";
            }
            else
            {
                Query += " and ParentAccountType is null";
            }
            Query+=" order by AccountType";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query ,
                    null);
            if (dsResult.Tables[0].Rows.Count ==  0)
            {
                return new AccountTypeCls[0];
            }
            AccountTypeCls[] CmsAccount = AccountTypeParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsAccount;
        }

        public static AccountTypeCls CreateAccountTypeModel(string SiteId, string AccountType)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableAccountType " +
                                               "     where AccountType=@AccountType",
                    new DbParam[] 
                    { 
                        new DbParam("AccountType", AccountType),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            AccountTypeCls OAccountType = AccountTypeParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OAccountType;
        }




        public static void AddAccountType(
            string SiteId,
            string ParentAccountType,
            string AccountType,
            string Description)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string AccountTypeId = System.Guid.NewGuid().ToString();
            if (string.IsNullOrEmpty(ParentAccountType)) ParentAccountType = null;
            DBService.Insert(null, "TableAccountType",
                    new DbParam[] 
                    { 
                        new DbParam("AccountType", AccountType),
                        new DbParam("Description", Description),
                        new DbParam("ParentAccountType", ParentAccountType),
                        new DbParam("LinkAccount", 1),
                    });
        }


        public static void SaveAccountType(string SiteId, string AccountType, string Description)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TableAccountType", "AccountType", AccountType,
                    new DbParam[] 
                    { 
                        new DbParam("Description", Description),
                    });
        }


        public static void DeleteAccountType(string SiteId, string AccountType)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableRoleAccountType where AccountType=@AccountType; "+
                " Delete from TableAccountType where AccountType=@AccountType ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("AccountType", AccountType),
                    });
        }

        public static void MoveRoleToAccountType(string SiteId, string[] AccountTypes, string RoleId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            IDbTransaction Trans = DBService.BeginTransaction();
            try
            {
                string Query =
                    " Delete from TableRoleAccountType where RoleId=@RoleId; ";
                DBService.ExecuteNonQuery(Trans, Query,
                            new DbParam[] 
                    { 
                        new DbParam("RoleId", RoleId),
                    });

                Query =
                    " Insert into TableRoleAccountType(RoleAccountTypeId, AccountType, RoleId) values (@RoleAccountTypeId, @AccountType, @RoleId)";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    string RoleAccountTypeId = System.Guid.NewGuid().ToString();
                    DBService.ExecuteNonQuery(Trans, Query,
                            new DbParam[] 
                    { 
                        new DbParam("RoleAccountTypeId", RoleAccountTypeId),
                        new DbParam("AccountType", AccountTypes[iIndex]),
                        new DbParam("RoleId", RoleId),
                    });
                }
                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
                throw (ex);
            }
        }


        public static AccountTypeCls[] ReadingAccountTypeForRole(string SiteId,  string RoleId)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query =
                " select TableAccountType.* from TableRoleAccountType " +
                "       join TableAccountType on TableAccountType.AccountType=TableRoleAccountType.AccountType" +
                "    where RoleId=@RoleId";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("RoleId", RoleId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new AccountTypeCls[0];
            }
            AccountTypeCls[] AccountTypes = AccountTypeParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return AccountTypes;
        }
    }
}

